package com.aizu.citybusdappe;

import java.util.ArrayList;
import java.util.regex.Pattern;

import com.aizu.citybusdappe.db.ParseCSVData;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class TabListAreaActivity extends ListActivity {
	private ArrayList<ItemStatus> items;
	private TabListAdapter adapter;
	private SQLiteDatabase db;
	static boolean isCalled = false;

	public TabListAreaActivity(){
		Log.d("TabListActivity","TabListArea constructor is called");
	}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		Log.d("TabListActivity","method onCreate id called");
		getListView().setClickable(true);
		getListView().setFastScrollEnabled(true);
		items = new ArrayList<ItemStatus>();
		Intent i = getIntent();
		String tag = i.getStringExtra("currentTag");
		Log.d("TabListActivity","tag is "+tag);
		createItems(tag);
        adapter = new TabListAdapter(this,R.id.listview,items);
        setListAdapter(adapter);
        registerForContextMenu(getListView());
    }


    public void createItems(String str){
		Log.d("TabListActivity","method createItems is called, str is "+str);

    	db = openOrCreateDatabase("busTimetableData", Context.MODE_PRIVATE , null);


    	//db.execSQL("PRAGMA encoding=Shift-JIS");
    	String sql = "select * from bus_stop where bsaddr = '"+str+"'";
    	//String sql = "select * from bus_lineage";

		Log.d("TabListActivity","rawQuery "+sql);

    	Cursor cursor = db.rawQuery(sql, null);

    	while(cursor.moveToNext()){
    		//this.items.add(new ItemStatus(cursor.getString(1)));
    		this.items.add(new ItemStatus(cursor.getInt(0),cursor.getString(1),cursor.getString(2),
    				cursor.getDouble(3),cursor.getDouble(4),cursor.getString(5)));
    	}
    	cursor.close();
    	db.close();
    }

}
